<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Method 3.2: Implement a Range Adaptor with arguments</title>
<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Range 2.0">
<link rel="up" href="../method_3.html" title="Method 3: provide range adaptor implementations">
<link rel="prev" href="method_3_1.html" title="Method 3.1: Implement a Range Adaptor without arguments">
<link rel="next" href="../../../style_guide.html" title="Terminology and style guidelines">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="method_3_1.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../method_3.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../../style_guide.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="range.reference.extending.method_3.method_3_2"></a><a class="link" href="method_3_2.html" title="Method 3.2: Implement a Range Adaptor with arguments">Method
          3.2: Implement a Range Adaptor with arguments</a>
</h5></div></div></div>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
                Provide a range for your return type, for example:
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">range</span><span class="special">/</span><span class="identifier">adaptor</span><span class="special">/</span><span class="identifier">argument_fwd</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">range</span><span class="special">/</span><span class="identifier">iterator_range</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">iterator</span><span class="special">/</span><span class="identifier">transform_iterator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Value</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">replace_value</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&amp;</span> <span class="identifier">result_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&amp;</span> <span class="identifier">argument_type</span><span class="special">;</span>

    <span class="identifier">replace_value</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&amp;</span> <span class="identifier">from</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&amp;</span> <span class="identifier">to</span><span class="special">)</span>
        <span class="special">:</span> <span class="identifier">m_from</span><span class="special">(</span><span class="identifier">from</span><span class="special">),</span> <span class="identifier">m_to</span><span class="special">(</span><span class="identifier">to</span><span class="special">)</span>
    <span class="special">{</span>
    <span class="special">}</span>

    <span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">Value</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="special">(</span><span class="identifier">x</span> <span class="special">==</span> <span class="identifier">m_from</span><span class="special">)</span> <span class="special">?</span> <span class="identifier">m_to</span> <span class="special">:</span> <span class="identifier">x</span><span class="special">;</span>
    <span class="special">}</span>
<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">Value</span> <span class="identifier">m_from</span><span class="special">;</span>
    <span class="identifier">Value</span> <span class="identifier">m_to</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Range</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">replace_range</span>
<span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">iterator_range</span><span class="special">&lt;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">transform_iterator</span><span class="special">&lt;</span>
        <span class="identifier">replace_value</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">Range</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;,</span>
        <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">Range</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="special">{</span>
<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">Range</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">value_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">Range</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">iterator_base</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">replace_value</span><span class="special">&lt;</span><span class="identifier">value_type</span><span class="special">&gt;</span> <span class="identifier">Fn</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">transform_iterator</span><span class="special">&lt;</span><span class="identifier">Fn</span><span class="special">,</span> <span class="identifier">iterator_base</span><span class="special">&gt;</span> <span class="identifier">replaced_iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">iterator_range</span><span class="special">&lt;</span><span class="identifier">replaced_iterator</span><span class="special">&gt;</span> <span class="identifier">base_t</span><span class="special">;</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">replace_range</span><span class="special">(</span><span class="identifier">Range</span><span class="special">&amp;</span> <span class="identifier">rng</span><span class="special">,</span> <span class="identifier">value_type</span> <span class="identifier">from</span><span class="special">,</span> <span class="identifier">value_type</span> <span class="identifier">to</span><span class="special">)</span>
        <span class="special">:</span> <span class="identifier">base_t</span><span class="special">(</span><span class="identifier">replaced_iterator</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">begin</span><span class="special">(</span><span class="identifier">rng</span><span class="special">),</span> <span class="identifier">Fn</span><span class="special">(</span><span class="identifier">from</span><span class="special">,</span><span class="identifier">to</span><span class="special">)),</span>
                 <span class="identifier">replaced_iterator</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">end</span><span class="special">(</span><span class="identifier">rng</span><span class="special">),</span> <span class="identifier">Fn</span><span class="special">(</span><span class="identifier">from</span><span class="special">,</span><span class="identifier">to</span><span class="special">)))</span>
     <span class="special">{</span>
     <span class="special">}</span>
 <span class="special">};</span>
</pre>
              </li>
<li class="listitem">
                Implement a holder class to hold the arguments required to construct
                the RangeAdaptor. The holder combines multiple parameters into one
                that can be passed as the right operand of <code class="computeroutput"><span class="keyword">operator</span><span class="special">|()</span></code>.
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">replace_holder</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_detail</span><span class="special">::</span><span class="identifier">holder2</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">replace_holder</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">from</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">to</span><span class="special">)</span>
        <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_detail</span><span class="special">::</span><span class="identifier">holder2</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">from</span><span class="special">,</span> <span class="identifier">to</span><span class="special">)</span>
    <span class="special">{</span> <span class="special">}</span>
<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">=(</span><span class="keyword">const</span> <span class="identifier">replace_holder</span><span class="special">&amp;);</span>
<span class="special">};</span>
</pre>
              </li>
<li class="listitem">
                Define an instance of the holder with the name of the adaptor
<pre class="programlisting"><span class="keyword">static</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_detail</span><span class="special">::</span><span class="identifier">forwarder2</span><span class="special">&lt;</span><span class="identifier">replace_holder</span><span class="special">&gt;</span>
<span class="identifier">replaced</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_detail</span><span class="special">::</span><span class="identifier">forwarder2</span><span class="special">&lt;</span><span class="identifier">replace_holder</span><span class="special">&gt;();</span>
</pre>
              </li>
<li class="listitem">
                Define <code class="computeroutput"><span class="keyword">operator</span><span class="special">|</span></code>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">SinglePassRange</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="identifier">replace_range</span><span class="special">&lt;</span><span class="identifier">SinglePassRange</span><span class="special">&gt;</span>
<span class="keyword">operator</span><span class="special">|(</span><span class="identifier">SinglePassRange</span><span class="special">&amp;</span> <span class="identifier">rng</span><span class="special">,</span>
          <span class="keyword">const</span> <span class="identifier">replace_holder</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;&amp;</span> <span class="identifier">f</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">replace_range</span><span class="special">&lt;</span><span class="identifier">SinglePassRange</span><span class="special">&gt;(</span><span class="identifier">rng</span><span class="special">,</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">val1</span><span class="special">,</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">val2</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">SinglePassRange</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="identifier">replace_range</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">SinglePassRange</span><span class="special">&gt;</span>
<span class="keyword">operator</span><span class="special">|(</span><span class="keyword">const</span> <span class="identifier">SinglePassRange</span><span class="special">&amp;</span> <span class="identifier">rng</span><span class="special">,</span>
          <span class="keyword">const</span> <span class="identifier">replace_holder</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_value</span><span class="special">&lt;</span><span class="identifier">SinglePassRange</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;&amp;</span> <span class="identifier">f</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">replace_range</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">SinglePassRange</span><span class="special">&gt;(</span><span class="identifier">rng</span><span class="special">,</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">val1</span><span class="special">,</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">val2</span><span class="special">);</span>
<span class="special">}</span>
</pre>
              </li>
</ol></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003-2010 Thorsten Ottosen,
      Neil Groves<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="method_3_1.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../method_3.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../../style_guide.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
